package com.wanmait.movies.dao;


import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

import com.wanmait.movies.util.DBHelper;
import com.wanmait.movies.vo.MovieCountry;
import com.wanmait.movies.vo.MovieLanguage;
import com.wanmait.movies.vo.MovieType;

public class MovieCountryDao {

    private MovieCountryDao() {};
	
	private static MovieCountryDao movieCountryDao = new MovieCountryDao();
	
	public static MovieCountryDao getDao() {
		return movieCountryDao;
	}
	
	//修改地区
	 public void updateCountry(String name,int id){
			
			Connection conn = DBHelper.getDBHelper().getconn();
			PreparedStatement ps =null;
			
			String sql = "UPDATE movie_country set country=? where id=? ";
			try {
				ps =conn.prepareStatement(sql);
				ps.setString(1, name);
				ps.setInt(2, id);
			
				ps.executeUpdate();
				
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			finally {
				DBHelper.getDBHelper().close(null, ps, conn);
			}

	   }
	   
	
	//通过id获得
	   public MovieCountry findById(int id){
			
			Connection conn = DBHelper.getDBHelper().getconn();
			PreparedStatement ps =null;
			ResultSet rs =null;
			
			String sql = "select * from movie_country where id=?";
			try {
				ps =conn.prepareStatement(sql);
				ps.setInt(1, id);
				
				rs=ps.executeQuery();
				if(rs.next()) {
				
				MovieCountry a= new MovieCountry();
				a.setCountry(rs.getString("country"));
				a.setId(id);
				
				return a;
				}
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			finally {
				DBHelper.getDBHelper().close(rs, ps, conn);
			}
			return null;
	   }
	
	//获得电影地区列表
	public ArrayList<MovieCountry> getCountryList(){
		ArrayList<MovieCountry> list =new ArrayList<MovieCountry>();
		
		Connection conn = DBHelper.getDBHelper().getconn();
		PreparedStatement ps =null;
		ResultSet rs =null;
		
		String sql ="select * from movie_country";
		
		try {
			ps = conn.prepareStatement(sql);
			
			rs = ps.executeQuery();
			
			while(rs.next()) {
				MovieCountry movieCountry =new MovieCountry();
				
				movieCountry.setId(rs.getInt("id"));
				movieCountry.setCountry(rs.getString("country"));
				
				list.add(movieCountry);
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		finally {
			DBHelper.getDBHelper().close(rs, ps, conn);
		}
		return list;
	}
	
	//添加新地区
	   public void add(String country){
			
			Connection conn = DBHelper.getDBHelper().getconn();
			PreparedStatement ps =null;
			ResultSet rs =null;
			
			String sql = "insert into movie_country(country) values(?)";
			try {
				ps =conn.prepareStatement(sql);
				ps.setString(1, country);
				
				ps.executeUpdate();
				
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			finally {
				DBHelper.getDBHelper().close(rs, ps, conn);
			}
	   }

	 //通过id获得地区
	   public MovieCountry getCountry(int id){
			
		   MovieCountry country = new MovieCountry();
			Connection conn = DBHelper.getDBHelper().getconn();
			PreparedStatement ps =null;
			ResultSet rs =null;
			
			String sql = "select * from movie_country where id=?";
			try {
				ps =conn.prepareStatement(sql);
				ps.setInt(1, id);
				
			while(rs.next()) {
				country.setCountry(rs.getString("country"));
				country.setId(id);
			}	
				
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			finally {
				DBHelper.getDBHelper().close(rs, ps, conn);
			}
			return country;
	}
	 //删除
	   public void deleteById(int id){
			
			Connection conn = DBHelper.getDBHelper().getconn();
			PreparedStatement ps =null;
			
			String sql = "delete from movie_country where id=?";
			try {
				ps =conn.prepareStatement(sql);
				ps.setInt(1, id);
			
				ps.executeUpdate();
				
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			finally {
				DBHelper.getDBHelper().close(null, ps, conn);
			}

	   }
}
